package cn.zyk.result;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

/**
 * @Author eHlw
 * @Date 2024/9/13 20:49
 * @description:
 */

@Data
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)// 当定义属性为null时不返回
public class Response<T> {
    private int code;

    private String msg;

    private T data;

    public static <T> Response<T> ok (int code, String msg, T data) {
        return setResult(code, msg, data);
    }

    public static <T> Response<T> ok (CodeEnum successEnum, T data) {
        return setResult(successEnum.getCode(), successEnum.getMsg(), data);
    }

    public static <T> Response<T> ok (T data) {
        return setResult(CodeEnum.GLOBAL_SEARCH_SUCCESS.getCode(), CodeEnum.GLOBAL_SEARCH_SUCCESS.getMsg(), data);
    }

    public static <T> Response<T> ok (CodeEnum successEnum) {
        return setResult(successEnum.getCode(), successEnum.getMsg(), null);
    }

    public static <T> Response<T> error(CodeEnum codeEnum) {
        return setResult(codeEnum.getCode(), codeEnum.getMsg(), null);
    }

    public static <T> Response<T> error(int code, String msg) {
        return setResult(code, msg, null);
    }

    private static <T> Response<T> setResult(int code, String msg, T data) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }
}
